package com.zjpg.forum.controller;

import com.zjpg.forum.common.AppResult;
import com.zjpg.forum.common.ResultCode;
import com.zjpg.forum.exception.ApplicationException;
import com.zjpg.forum.model.Board;
import com.zjpg.forum.services.IBoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName BoardController
 * @Description: TODO
 * @Author: JCC
 * @CreateTime: 2023/8/15 16:39
 */
@Api(tags = "板块接口")
@Slf4j
@RestController
@RequestMapping("/board")
public class BoardController {

    // 从配置文件中读取值，如果没有配置值，默认值为9
    @Value("${zjpg.forum.index.board-num:9}")
    private Integer indexBoardNum;

    @Resource
    private IBoardService boardService;

    /**
     * 查询首页板块列表
     * @return
     */
    @ApiOperation("获取首页板块列表")
    @GetMapping("/toplist")
    public AppResult<List<Board>> toplist() {
        log.info("首页板块个数为：" + indexBoardNum);
        // 调用Service查询
        List<Board> boards = boardService.selectByNum(indexBoardNum);
        // 判断是否为空
        if (boards == null) {
            boards = new ArrayList<>();
        }
        // 返回结果
        return AppResult.success(boards);
    }

    @ApiOperation("获取板块信息")
    @GetMapping("/getbyid")
    public AppResult<Board> getById(@ApiParam("板块ID") @RequestParam("boardId") @NonNull Long id) {
        Board board = boardService.selectById(id);
        if (board == null || board.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        return AppResult.success(board);
    }
}
